package com.decent.ejfadmin.apply.controller;

import com.decent.ejfadmin.apply.request.RebateApplyQueryParam;
import com.decent.ejfadmin.apply.service.RebateApplyService;
import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.exception.SupException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 推荐有礼申请
 *
 * @author zhangchao
 * @date 2018/7/30 15:31
 */
@RestController
@RequestMapping("apply")
@Slf4j
public class RebateApplyController {

    @Autowired
    private RebateApplyService rebateApplyService;

    /**
     * 推荐有礼申请查询分页数据
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "findRebateApplyPage",method = RequestMethod.GET)
    public Page findRebateApplyPage(RebateApplyQueryParam param) {
        Page page = new Page();
        try {
            page = rebateApplyService.findRebateApplyPage(param);
        } catch (Exception e) {
            page.setTotal(0);
            log.warn("RebateApplyController findRebateApplyPage occurs error Internal Server Error  ");
        }
        return page;
    }

    @RequestMapping(value = "changeState", method = RequestMethod.POST)
    public MessageBean changeState(Long applyId,Integer state) {
        MessageBean messageBean = MessageBean.success();
        try {
            rebateApplyService.changeState(applyId,state);
        } catch (SupException e) {
            messageBean = MessageBean.fail(e.getResMessage());
            log.warn("RebateApplyController changeState occurs error Internal Server Error  " + e.getResMessage());
        } catch (Exception e) {
            messageBean = MessageBean.fail();
            log.warn("RebateApplyController changeState occurs error Internal Server Error  ");
        }
        return messageBean;
    }

    @RequestMapping(value = "addRemark", method = RequestMethod.POST)
    public MessageBean addRemark(Long applyId,String remark) {
        MessageBean messageBean = MessageBean.success();
        try {
            rebateApplyService.addRemark(applyId,remark);
        } catch (SupException e) {
            messageBean = MessageBean.fail(e.getResMessage());
            log.warn("RebateApplyController addRemark occurs error Internal Server Error  " + e.getResMessage());
        } catch (Exception e) {
            messageBean = MessageBean.fail();
            log.warn("RebateApplyController addRemark occurs error Internal Server Error  ");
        }
        return messageBean;
    }

}
